<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>忘记密码</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" href="../../layuiadmin/layui/css/layui.css" media="all">
    <!--<link rel="stylesheet" th:href="@{/layuiadmin/style/admin.css}" href="../../layuiadmin/style/admin.css" media="all">-->
    <link rel="stylesheet" th:href="@{/layuiadmin/style/login.css}" href="../../layuiadmin/style/login.css" media="all">
</head>
<body>
<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">
    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2>找回密码</h2>
        </div>
        <div class="layadmin-user-login-box layadmin-user-login-body layui-form">

            <script type="text/html" template>
                {{# if(layui.router().search.type === 'resetpass'){ }}
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="LAY-user-login-password"></label>
                    <input type="password" name="password" id="LAY-user-login-password" lay-verify="pass" placeholder="新密码" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="LAY-user-login-repass"></label>
                    <input type="password" name="repass" id="LAY-user-login-repass" lay-verify="required" placeholder="确认密码" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-forget-resetpass">重置新密码</button>
                </div>
                {{# } else { }}
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-star" for="LAY-user-login-email"></label>
                    <input type="text" name="email" id="LAY-user-login-email" lay-verify="email" placeholder="请输入注册时的邮箱" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <div class="layui-row">
                        <div class="layui-col-xs7">
                            <label class="layadmin-user-login-icon layui-icon layui-icon-vercode" for="LAY-user-login-smscode"></label>
                            <input type="text" name="vercode" id="LAY-user-login-smscode" lay-verify="required" placeholder="验证码" class="layui-input">
                        </div>
                        <div class="layui-col-xs5">
                            <div style="margin-left: 10px;">
                                <button type="button" onclick="sendemail();" class="layui-btn layui-btn-primary layui-btn-fluid" id="getsmscode">
                                    <div id="text">获取验证码</div>
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="LAY-user-login-password"></label>
                    <input type="password" name="password" id="LAY-user-login-password" lay-verify="pass" placeholder="新密码" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-forget-submit">重置密码</button>
                </div>
                {{# } }}
            </script>

        </div>
    </div>

    <div class="layui-trans layadmin-user-login-footer">
        <p>© 2018 <a href="#" target="_blank">xingying.com</a></p>
    </div>

</div>

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

<script type="text/javascript">

    function sendemail() {
        var email = document.getElementsByName("email")[0];
        var t1 = /^\s*$/;
        if(t1.test(email.value)) {
            layer.msg('请输入邮箱', {
                time: 1500,
                icon: 2,
                offset: '134px'
            });
            email.focus();
            return;
        }
        var object = new Object(); //创建一个存放数据的对象
        object["email"] = email.value;
        var email = email.value;
        $.ajax({
            url: "http://127.0.0.1:8080/user/sendEmail/"+email,
            type: "post",
            success: function(res) {
                layer.msg(res, {
                    time: 2500,
                    icon: 1,
                    offset: '50px'
                });
            }
        });
        layer.msg('发送成功<br>如果没有收到请等待60秒后再次获取', {
            time: 2500,
            icon: 1,
            offset: '50px'
        });
        var btn = document.getElementById("getsmscode");
        var text = document.getElementById("text");
        btn.disabled = true;
        var time = 60;
        var set = setInterval(function() {
            text.innerHTML = "请等待" + (--time) + "秒";
        }, 1000);
        setTimeout(function() {
            btn.disabled = false;
            clearInterval(set);
            text.innerHTML = "获取验证码";
        }, 60000);
    }
</script>

<script th:src="@{/layuiadmin/layui/layui.js}" src="../../layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'user'], function(){
        var $ = layui.$
            ,setter = layui.setter
            ,admin = layui.admin
            ,form = layui.form
            ,router = layui.router();

        form.render();

        //找回密码下一步
        form.on('submit(LAY-user-forget-submit)', function(obj){

            var vercode = obj.field.vercode;
            var password = obj.field.password

            //请求接口,验证邮箱验证码是否正确
            $.ajax({
                url: '/user/forget/'+ vercode //实际使用请改成服务端真实接口
                ,data: JSON.stringify(obj.field)
                ,dataType: "json"
                ,contentType: "application/json; charset=utf-8"
                ,type: "post"
                ,success: function(res){
                    if (res.code ==0){
                        layer.msg('密码已成功重置', {
                            offset: '15px'
                            ,icon: 1
                            ,time: 1000
                        }, function(){
                            location.href = 'login.html'; //跳转到登入页
                        });
                    }else {
                        layer.msg('验证码错误', {
                            icon: 5
                        });
                    }

                }
            });

            return false;
        });

    });
</script>
</body>
</html>